class Util {
    serialize(obj) {
        var str = [];
        for (var p in obj)
            if (obj.hasOwnProperty(p)) {
                str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
            }
        return str.join("&");
    }

}
class Method {
    constructor() { }
    //番号搜索
    search_codes(code) {
        have = false;
        GM_xmlhttpRequest({
            method: 'GET',
            url: "https://zhongziso6.com/list/" + code + '/1',
            onload: function (responseDetails) {
                var responseData = responseDetails.response;
                var magnet = responseData.match(/[0-9a-zA-Z]{40,}/);
                console.log("番号对应magnet磁链: magnet:?xt=urn:btih:" + magnet);
                if (magnet != null) {
                    this.addToboard("magnet:?xt=urn:btih:" + magnet, code);
                } else {
                    this.addToboard("该磁链暂无下载地址", code);
                }

            }
        });
        have = true;
        return have;
    }
    checkReapt(arr, key) {
        let temp = []
        arr.forEach(item => {
            temp.push(item.includes(key))
        })
        return temp.includes(true);
    }
    //番号检测
    check_codes() {
        var codes = $("body").text().match(/[a-zA-Z]{2,8}-[0-9]{2,8}/g);
        let proArr = [];
        if (codes) {


            console.log(codes);
            codes = Array.from(new Set(codes.map(item => item.toUpperCase())))
            let noShould = ['PUB','UA','PPV']
            for (var index = 0; index <= codes.length - 1; index++) {
                noShould.forEach(noItem => {
                    if (codes[index].toUpperCase().includes(noItem)) {
                        codes.splice(index, 1);
                        index--;

                    }
                })
            };

            console.log(codes);

            for (const code of codes) {
                proArr.push(new Promise(resolve => {
                    GM_xmlhttpRequest({
                        method: 'GET',
                        url: "https://zhongziso6.com/list/" + code + '/1',
                        onload: function (responseDetails) {
                            var responseData = responseDetails.response;
                            var magnet = responseData.match(/[0-9a-zA-Z]{40,}/);
                            if (magnet != null) {
                                resolve({ link: "magnet:?xt=urn:btih:" + magnet, name: code });
                            } else {
                                resolve({ link: "该磁链暂无下载地址", name: code });
                            }

                        }
                    });
                }))
            }
        }
        return Promise.all(proArr)
    }

    //magnet链接检测
    check_magnets() {
        var magnets = $("body").html().match(/[0-9a-zA-Z]{40,}/g);
        let result = [];
        if (magnets) {
            $.each(magnets, function (i, n) {
                var magnet = n;
                if (magnet.length == 40) {
                    // console.log("发现magnet磁链: magnet:?xt=urn:btih:" + magnet);
                    result.push({ link: "magnet:?xt=urn:btih:" + magnet, name: "未知" });
                }
            });
        }
        return result;
    }

    //ed2k链接检测
    check_ed2ks() {
        var ed2ks = $("body").html().match(/ed2k:\/\/[^'"]*/g);
        let result = [];
        if (ed2ks) {
            $.each(ed2ks, function (i, n) {
                var ed2k = n;
                // console.log("发现ed2k: " + ed2k);
                result.push({ link: ed2k, name: "未知" });
            });
        }
        return result;
    }
    //thunder链接检测
    check_thunder() {
        var thunders = $("body").html().match(/thunder:\/\/[^'"]*/g);
        let result = [];
        if (thunders) {
            $.each(thunders, function (i, n) {
                var thunder = n;
                // console.log("发现thunder: " + thunder);
                result.push({ link: thunder, name: "未知" });
            });
        }
        return result;
    }
    //查找所有
    async checkAll() {
        let codeMagnet = await this.check_codes()
        return [...codeMagnet, ...this.check_magnets(), ...this.check_ed2ks(), ...this.check_thunder()]
    }
    //复制链接
    copy_link(link) {
        link.select();
        document.execCommand("copy");
        alert("复制成功\n\n如浏览器不支持此操作，请手动复制");

    }
}
class Gm {
    constructor() {
    }
    //方法: 通用chrome通知
    notifiy(title, body, icon, click_url) {
        GM_notification({
            text: body,
            title: title,
            timeout: 10000,
            image: icon,
            onclick: function () {
                window.open(click_url);
            }
        });
    }
    http(url, method, data, headers) {
        // GM_xmlhttpRequest({
        //     method: 'GET',
        //     url: "https://zhongziso6.com/list/" + code + '/1',
        //     onload: function (responseDetails) {
        //         var responseData = responseDetails.response;
        //         var magnet = responseData.match(/[0-9a-zA-Z]{40,}/);
        //         console.log("https://zhongziso6.com/list/" + code);
        //         console.log("番号对应magnet磁链: magnet:?xt=urn:btih:" + magnet);
        //         if (magnet != null) {
        //             this.addToboard("magnet:?xt=urn:btih:" + magnet, code);
        //         } else {
        //             this.addToboard("该磁链暂无下载地址", code);
        //         }

        //     }
        // });
        return new Promise(resolve => {
            let httpSetupObj = {
                method,
                url,
                onload: function (responseDetails) {
                    resolve(responseDetails)
                }
            }
            if (headers) {
                httpSetupObj = Object.assign(httpSetupObj, { headers: headers })
            }
            if (method.toUpperCase().includes('POST')) {
                httpSetupObj = Object.assign(httpSetupObj, { data: data })
            }
            GM_xmlhttpRequest(httpSetupObj);
        })


    }

}
